<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/30
 * Time: 15:13
 */
namespace App\Modules\User\Services;

use App\Modules\User\Models\UserRecommend;
use Illuminate\Database\Eloquent\Model;
use DB;
use Redis;

class UserRecommendService extends Model{

    /**
     * 获取自荐总信息
     *
     */
    static function getSelfRecInfo($uid){
        $self_rec_times = self::RecommendTimes($uid);
        $self_rec_times_all = UserRecommendAllService::RecommendAllTimes();

        $self_view_rate = $self_rec_times==0?0:round(100*round(self::RecommendReadTimes($uid)/$self_rec_times,4),2);
        $self_view_rate_average = $self_rec_times_all==0?0:round(100*round(UserRecommendAllService::RecommendAllReadTimes()/$self_rec_times_all,4),2);
        $self_view_rate_diff = $self_view_rate - $self_view_rate_average;
        $self_view_rate_level = 'medium';
        if($self_view_rate_diff>1) $self_view_rate_level='high';
        if($self_view_rate_diff<-1) $self_view_rate_level='low';

        $self_rec_user_num = UserRecommendAllService::RecommendAllUserNum();
        $selfreco_num   = $self_rec_times;
        $selfreco_num_average = $self_rec_user_num==0?0:round($self_rec_times_all/$self_rec_user_num,0);
        $selfreco_num_diff = $selfreco_num-$selfreco_num_average;
        $selfreco_num_level = 'medium';
        if($selfreco_num_diff>0) $selfreco_num_level = 'high';
        if($selfreco_num_diff<0) $selfreco_num_level = 'low';

        $selfreco_suc_num = self::RecommendSuccessTimes($uid);
        $selfreco_suc_average = $self_rec_user_num==0?0:round(UserRecommendAllService::RecommendAllSuccessTimes()/$self_rec_user_num,0);
        $selfreco_suc_diff  =   $selfreco_suc_average-$selfreco_suc_num;
        $selfreco_suc_level = 'medium';
        if($selfreco_suc_diff>0) $selfreco_suc_level = 'high';
        if($selfreco_suc_diff<0) $selfreco_suc_level = 'low';

        return [
            'self_view_rate'=>$self_view_rate,
            'self_view_rate_level'=>$self_view_rate_level,
            'selfreco_num'=>$selfreco_num,
            'selfreco_num_level'=>$selfreco_num_level,
            'selfreco_suc_num'=>$selfreco_suc_num,
            'selfreco_suc_level'=>$selfreco_suc_level
        ];

    }

    /**
     * 自荐表被查看次数
     */
    static function RecommendReadTimes($uid){
        $readTime = Redis::hget('self_recommend_info:'.$uid, 'readTimes');

        if ($readTime === null ) {
            $readTime = UserRecommend::RecommendReadTimes($uid);
            Redis::hset('self_recommend_info:'.$uid, 'readTimes',$readTime);
        }

        return $readTime;
    }

    /**
     * 用户自荐表数
     */
    static function RecommendTimes($uid){
        $recommendTimes = Redis::hget('self_recommend_info:'.$uid, 'recommendTimes');

        if ($recommendTimes === null ) {
            $recommendTimes = UserRecommend::RecommendTimes($uid);
            Redis::hset('self_recommend_info:'.$uid, 'recommendTimes',$recommendTimes);
        }

        return $recommendTimes;
    }
    /**
     * 用户自荐成功次数
     */
    static function RecommendSuccessTimes($uid){
        $RecommendSuccessTimes = Redis::hget('self_recommend_info:'.$uid, 'recommendSuccessTimes');

        if ($RecommendSuccessTimes === null ) {
            $RecommendSuccessTimes = UserRecommend::RecommendSuccessTimes($uid);
            Redis::hset('self_recommend_info:'.$uid, 'recommendSuccessTimes',$RecommendSuccessTimes);
        }

        return $RecommendSuccessTimes;
    }

}